import Vue from 'vue'
import Router from 'vue-router'
import { getVuexState } from '@/utils/public.js'

//组件模块

import Login from './components/login'

import Container from './business/baseinfo/container'
import Warehouse from './business/baseinfo/warehouse'
import Temporary from './business/baseinfo/temporary'
import Partition from './business/baseinfo/partition'
import PartitionList from './business/baseinfo/partitionList'
import Location from './business/baseinfo/location'
import ArrivalNotice from './business/warehouseManagement/arrivalNotice/arrivalnotice'
import Regional from './business/baseinfo/regional'
import ArrivalPrint from './business/warehouseManagement/arrivalNotice/arrivalprint'
import Quality from './business/warehouseManagement/quality/quality'
import PrintSku from './business/warehouseManagement/quality/printSku'
import PrintQuality from './business/warehouseManagement/quality/printQuality'
import QualityHistory from './business/warehouseManagement/quality/qualityHistory'
import Order from './business/warehouseManagement/orderManagement/order'
import OrderInfo from './business/warehouseManagement/orderManagement/orderInfo'
import OrderHistory from './business/warehouseManagement/orderManagement/orderHistory'

import Erro404 from './err/erro404'

Vue.use(Router)
import store from './store'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location){
  return originalPush.call(this, location).catch((err) => err)
}

let router = new Router({
  mode:'hash',
  routes:[
    { path:'/', name:'login', component:Login },
    {
      path:'/home',
      name:'home',
      component:() =>
        import('@/components/global/home'),
      children:[ // 嵌套子路由
        {
          path:'index', // 用户登录后首页
          name:'index',
          component:() =>
            import('@/business/index'),
          meta:{ keepAlive:true }
        },
        {
          path:'configure', // 用户配置
          name:'configure',
          component:() =>
            import('@/business/userConfig/configure'),
          meta:{ keepAlive:false }
        },
        {
          path:'warehouse',
          name:'warehouse',
          component:Warehouse,
          meta:{ keepAlive:true }
        },
        {
          path:'temporary',
          name:'temporary',
          component:Temporary,
          meta:{ keepAlive:true }
        },
        {
          path:'container',
          name:'container',
          component:Container,
          meta:{ keepAlive:true }
        }, {
          path:'location',
          name:'location',
          component:Location,
          meta:{ keepAlive:true }
        }, {
          path:'partition',
          name:'partition',
          component:Partition,
          meta:{ keepAlive:true }
        }, {
          path:'partition/partitionList',
          name:'partitionList',
          component:PartitionList,
          meta:{ keepAlive:true }
        },
        {
          path:'arrivalnotice',
          name:'arrivalnotice',
          component:ArrivalNotice,
          meta:{ keepAlive:true }
        },
        {
          path:'newArrivalNotice',
          name:'newArrivalNotice',
          component:() =>
            import('./business/warehouseManagement/arrivalNotice/newArrivalNotice'),
          meta:{ keepAlive:true }
        },
        {
          path:'purchase',
          name:'purchase',
          component:() =>
            import('./business/warehouseManagement/purchaseManagement/purchase'),
          meta:{ keepAlive:true }
        },
        {
          path:'sign',
          name:'sign',
          component:() =>
            import('./business/signingManagement/sign'),
          meta:{ keepAlive:false }
        },
        {
          path:'regional',
          name:'regional',
          component:Regional,
          meta:{ keepAlive:true }
        },
        {
          path:'regional/addregion',
          name:'addregion',
          component:() =>
            import('@/business/baseinfo/addRegion'),
          meta:{ keepAlive:false }
        },
        {
          path:'quality',
          name:'quality',
          component:Quality,
          meta:{ keepAlive:true }
        },
        {
          path:'quality/printSku',
          name:'printSku',
          component:PrintSku,
          meta:{ keepAlive:true }
        },
        {
          path:'quality/printQuality',
          name:'printQuality',
          component:PrintQuality,
          meta:{ keepAlive:true }
        },
        {
          path:'qualityHistory',
          name:'qualityHistory',
          component:QualityHistory,
          meta:{ keepAlive:true }
        },
        {

          path:'signing', // 理货
          name:'signing',
          component:() =>
            import('@/business/signingManagement/signing'),
          meta:{ keepAlive:true }
        }, {
          path:'signingPrint',
          name:'signingPrint',
          component:() =>
            import('@/business/signingManagement/signingPrint'),
          meta:{ keepAlive:true }
        },
        {
          path:'order',
          name:'order',
          component:Order,
          meta:{ keepAlive:true }
        },
        {
          path:'order/orderInfo',
          name:'orderInfo',
          component:OrderInfo,
          meta:{ keepAlive:false }
        }, {
          path:'orderHistory',
          name:'orderHistory',
          component:OrderHistory,
          meta:{ keepAlive:true }
        }, {
          path:'onShelves', // 上架
          name:'onShelves',
          component:() =>
            import('@/business/warehouseManagement/shelves/onShelves'),
          meta:{ keepAlive:true }
        }, {
          path:'shelvesHistory', // 上架历史
          name:'shelvesHistory',
          component:() =>
            import('@/business/warehouseManagement/shelves/shelvesHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'onShelvesTask', // 上架任务
          name:'onShelvesTask',
          component:() =>
            import('@/business/warehouseManagement/shelves/onShelvesTask'),
          meta:{ keepAlive:true }
        }, {
          path:'shelvesTaskHistory', // 上架任务历史
          name:'shelvesTaskHistory',
          component:() =>
            import('@/business/warehouseManagement/shelves/shelvesTaskHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'picktask', // 拣货任务
          name:'picktask',
          component:() =>
            import('@/business/warehouseManagement/picktask/picktask'),
          meta:{ keepAlive:true }
        }, {
          path:'picktaskHistory', // 拣货历史任务
          name:'picktaskHistory',
          component:() =>
            import('@/business/warehouseManagement/picktask/picktaskHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'picktaskParticular', // 拣货任务明细
          name:'picktaskParticular',
          component:() =>
            import('@/business/warehouseManagement/picktask/picktaskParticular'),
          meta:{ keepAlive:true }
        }, {
          path:'picktaskParticularHistory', // 拣货任务明细历史
          name:'picktaskParticularHistory',
          component:() =>
            import('@/business/warehouseManagement/picktask/picktaskParticularHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'reduction', // 还货上架
          name:'reduction',
          component:() =>
            import('@/business/warehouseManagement/reduction/reduction'),
          meta:{ keepAlive:true }
        },
        {
          path:'wave',
          name:'wave',
          component:() =>
            import('@/business/warehouseManagement/wave/wave'),
          meta:{ keepAlive:true }
        },
        {
          path:'waveHistory',
          name:'waveHistory',
          component:() =>
            import('@/business/warehouseManagement/wave/waveHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'sorting', // 二次分拣
          name:'sorting',
          component:() =>
            import('@/business/warehouseManagement/sorting/sorting'),
          meta:{ keepAlive:true }
        }, {
          path:'sortinghistory', // 二次分拣历史
          name:'sortinghistory',
          component:() =>
            import('@/business/warehouseManagement/sorting/sortinghistory'),
          meta:{ keepAlive:true }
        }, {
          path:'stockInfo', // 库位库存
          name:'stockInfo',
          component:() =>
            import('@/business/stock/stockInfo'),
          meta:{ keepAlive:true }
        }, {
          path:'productInventory', // 产品库存
          name:'productInventory',
          component:() =>
            import('@/business/stock/productInventory'),
          meta:{ keepAlive:true }
        }, {
          path:'stockHistory', // 库存变动历史
          name:'stockHistory',
          component:() =>
            import('@/business/stock/stockHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'placeHolderStock', // 占位库存历史
          name:'placeHolderStock',
          component:() =>
            import('@/business/stock/placeHolderStock'),
          meta:{ keepAlive:true }
        }, {
          path:'reviewpackaging',
          name:'reviewpackaging',
          component:() =>
            import('@/business/warehouseManagement/reviewPackaging/reviewpackaging'),
          meta:{ keepAlive:true }
        }, {
          path:'review',
          name:'review',
          component:() =>
            import('@/business/warehouseManagement/reviewPackaging/review'),
          meta:{ keepAlive:true }
        }, {
          path:'reviewHistory',
          name:'reviewHistory',
          component:() =>
            import('@/business/warehouseManagement/reviewPackaging/reviewHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'packaging',
          name:'packaging',
          component:() =>
            import('@/business/warehouseManagement/reviewPackaging/packaging'),
          meta:{ keepAlive:true }
        }, {
          path:'packagingHistory',
          name:'packagingHistory',
          component:() =>
            import('@/business/warehouseManagement/reviewPackaging/packagingHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'sending',
          name:'sending',
          component:() =>
            import('@/business/warehouseManagement/sending/sending'),
          meta:{ keepAlive:true }
        }, {
          path:'sendingHistory',
          name:'sendingHistory',
          component:() =>
            import('@/business/warehouseManagement/sending/sendingHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'putInStorage', // 其他入库
          name:'putInStorage',
          component:() =>
            import('@/business/otherStorage/putInStorage'),
          meta:{ keepAlive:true }
        }, {
          path:'checkoutStorage', // 其他出库
          name:'checkoutStorage',
          component:() =>
            import('@/business/otherStorage/checkoutStorage'),
          meta:{ keepAlive:true }
        },
        {
          path:'waveStrategy',
          name:'waveStrategy',
          component:() =>
            import('@/business/strategyManagement/wave'),
          meta:{ keepAlive:true }
        }, {
          path:'printerService',
          name:'printerService',
          component:() =>
            import('@/business/serviceManagement/printerService'),
          meta:{ keepAlive:true }
        }, {
          path:'printService',
          name:'printService',
          component:() =>
            import('@/business/serviceManagement/printService'),
          meta:{ keepAlive:true }
        },
        {
          path:'difference', // 产品库存
          name:'difference',
          component:() =>
            import('@/business/baseinfo/difference'),
          meta:{ keepAlive:true }
        }, {
          path:'inventory', // 盘点
          name:'inventory',
          component:() =>
            import('@/business/baseinfo/inventory'),
          meta:{ keepAlive:true }
        }, {
          path:'inventoryHistory', // 盘点历史
          name:'inventoryHistory',
          component:() =>
            import('@/business/baseinfo/inventoryHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'initialInventory', // 初盘单
          name:'initialInventory',
          component:() =>
            import('@/business/baseinfo/initialInventory'),
          meta:{ keepAlive:true }
        }, {
          path:'initialInventoryHistory', // 初盘历史
          name:'initialInventoryHistory',
          component:() =>
            import('@/business/baseinfo/initialInventoryHistory'),
          meta:{ keepAlive:true }
        }, {
          path:'locationTypeSetting',
          name:'locationTypeSetting',
          component:() =>
            import('@/business/baseinfo/locationTypeSetting'),
          meta:{ keepAlive:true }
        }, {
          path:'containerTypeSetting',
          name:'containerTypeSetting',
          component:() =>
            import('@/business/baseinfo/containerTypeSetting'),
          meta:{ keepAlive:true }
        }, {
          path:'transposing',
          name:'transposing',
          component:() =>
            import('@/business/warehouseManagement/transposing/async-component'),
          meta:{ keepAlive:true }
        }, {
          path:'transposingHistory',
          name:'transposingHistory',
          component:() =>
            import('@/business/warehouseManagement/transposing/async-history'),
          meta:{ keepAlive:true }
        },
        {
          path:'areashift',
          name:'areashift',
          component:() =>
            import('@/components/areashift/async-component'),
          meta:{ keepAlive:true }
        },
        {
          path:'takeoff',
          name:'takeoff',
          component:() =>
            import('@/components/areashift/takeoff'),
          meta:{ keepAlive:true }
        }, {
          path:'takeoffInfo',
          name:'takeoffInfo',
          component:() =>
            import('@/components/areashift/takeoffinfo'),
          meta:{ keepAlive:false }
        }, {
          path:'take',
          name:'take',
          component:() =>
            import('@/components/areashift/take'),
          meta:{ keepAlive:false }
        }, {
          path:'putaway',
          name:'putaway',
          component:() =>
            import('@/components/areashift/putaway'),
          meta:{ keepAlive:true }
        }, {
          path:'putawayInfo',
          name:'putawayfInfo',
          component:() =>
            import('@/components/areashift/putawayInfo'),
          meta:{ keepAlive:false }
        }, {
          path:'put',
          name:'put',
          component:() =>
            import('@/components/areashift/put'),
          meta:{ keepAlive:false }
        },
        {
          path:'areashiftHistory',
          name:'areashiftHistory',
          component:() =>
            import('@/components/areashift/async-history'),
          meta:{ keepAlive:true }
        },
        {
          path:'forklift', // 叉车使用规则
          name:'forklift',
          component:() =>
            import('@/business/baseinfo/forklift'),
          meta:{ keepAlive:true }
        },
        {
          path:'scheduling',
          name:'scheduling',
          component:() =>
            import('@/business/baseinfo/scheduling/async-component'),
          meta:{ keepAlive:true }
        },
        {
          path:'log',
          name:'log',
          component:() =>
            import('@/business/baseinfo/scheduling/log'),
          meta:{ keepAlive:true }
        }, {
          path:'pda',
          name:'pda',
          component:() =>
            import('@/business/pda'),
          meta:{ keepAlive:false }
        },
        {
          path:'synchronization',
          name:'synchronization',
          component:() =>
            import('@/business/synchronizationLog/async-component'),
          meta:{ keepAlive:true }
        }
      ]
    },
    {
      path:'/print', // 到货通知，打印
      name:'print',
      component:ArrivalPrint
    },
    {
      path:'*',
      component:Erro404
    }

  ]
})

router.beforeEach((to, from, next) => {

  let path = to.path.substring(to.path.lastIndexOf('/') + 1)
  if (!path){
    getVuexState() // 针对浏览器刷新操作（F5），合并vuex的state
    if (store.state.basicInfo.userInfo.id){ // 如果用户是登录状态访问登录页，自动跳转到默认首页
      next({ name:'index' })
    } else {
      next()
    }
  } else if (['print'].includes(path)){
    next()
  } else if (['index'].includes(path)){
    getVuexState() // 针对浏览器刷新操作（F5），合并vuex的state
    let userId = store.state.basicInfo.userInfo.id
    if (!userId){
      next({ name:'login' })
    } else {
      next()
    }
  } else {
    let permission = store.state.permission.permission || []
    let currentPagePermission = ''
    if (!permission.length){
      getVuexState() // 针对浏览器刷新操作（F5），合并vuex的state
      let userId = store.state.basicInfo.userInfo.id
      if (!userId){
        next({
          name:'login',
          query:{ redirect:to.name }
        })
      } else {
        store.dispatch('permission/actionSetPermission', userId).then(() => {
          permission = store.state.permission.permission
          currentPagePermission = permission.find((item) => item.page === path)
          if (!currentPagePermission){
            next({ name:'index' })
          } else {
            store.commit('permission/setCurrentPagePermission', currentPagePermission)
            next()
          }
          next()
        })
      }
    } else {
      currentPagePermission = permission.find((item) => item.page === path)
      if(currentPagePermission)
        store.commit('permission/setCurrentPagePermission', currentPagePermission)
      next()
    }

  }
})
export default router